
import delimited "$raw/acs5yr/aff_download/ACS_14_5YR_B02001_with_ann.csv", delimiter(comma) varnames(2) clear 
	drop margin* v21 v23
	gen oth = estimatetotal-estimatetotalwhitealone-estimatetotalblackorafricanameri
	rename estimatetotal tot
	rename estimatetotalwhitealone wht
	rename estimatetotalblackorafricanameri blk
	keep id2 geography tot wht blk oth
	replace wht = wht/tot
	replace blk = blk/tot
	replace oth = oth/tot
	tempfile dem
	save `dem'
	
import delimited "$raw/acs5yr/aff_download/ACS_14_5YR_S0101_with_ann.csv", delimiter(comma) varnames(2) clear 
	keep maleestimatetotalpopulation femaleestimatetotalpopulation id2 geography totalestimatetotalpopulation
	rename male male
	rename female female
	replace male = male/total
	replace female = female/total
	drop total
	tempfile gen
	save `gen'
	
import delimited "$raw/acs5yr/aff_download/ACS_14_5YR_S1903_with_ann.csv", delimiter(comma) varnames(2) clear 
	keep totalestimatehouseholds medianincomedollarsestimatehouse totalestimatehouseholdsoneracewh totalestimatehouseholdsoneracebl v10 v14 id2 geography 
	rename totalestimatehouseholds households
	rename medianincomedollarsestimatehouse medinc
	rename totalestimatehouseholdsoneracewh whhouseholds
	rename totalestimatehouseholdsoneracebl blhouseholds
	rename v10 medincwh
	rename v14 medincbl
	
	destring medincbl, gen(temp) force
	sum blhouseholds if mi(temp)
	sum blhouseholds if !mi(temp)
	drop *wh* *bl* temp

	
	merge 1:1 id2 geography using `dem', assert(3) nogen
	merge 1:1 id2 geography using `gen', assert(3) nogen
	
	rename id2 countyid
	tempfile acs
	save `acs'
	
	
	// IAT data with ANY implicit bias data
	use "$data/iat", clear
	keep if !mi(implicitbias)
	collapse (mean) implicitbias, by(countyid)
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(1)
	tempfile iat
	save `iat'
	
	// IAT data with K12 implicit bias data
	use "$data/iat", clear
	keep if !mi(implicitbias)
	collapse (mean) implicitbias (max) k12, by(countyid)
	keep if k12==1
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(2)
	tempfile k12
	save `k12'
	
	// SEDA gap data 
	use "$data/iat", clear
	collapse (mean) implicitbias, by(countyid)
	merge 1:1 countyid using "$data/sedagaps", keep(3) nogen
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(3)
	tempfile gap
	save `gap'

	// OCR data 
	use "$data/iat", clear
	collapse (mean) implicitbias, by(countyid)
	merge 1:1 countyid using "$data/1216_countylevel_clean", keep(3) nogen
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(4)
	tempfile ocr
	save `ocr'
	
	// Pooled 
	use "$data/iat", clear
	collapse (mean) implicitbias, by(countyid)
	merge 1:1 countyid using "$data/commonsamp", keep(3) nogen
	keep if pooledsamp==1
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(5)
	tempfile pool
	save `pool'
	
	// Teach 
	use "$data/iat", clear
	collapse (mean) implicitbias, by(countyid)
	merge 1:1 countyid using "$data/commonsamp", keep(3) nogen
	keep if tchsamp==1
	keep countyid implicitbias
	merge 1:1 countyid using `acs', keep(3) nogen 
	renvars households medinc tot wht blk oth male female implicitbias, suffix(6)
	
	merge 1:1 countyid using `iat', nogen
	merge 1:1 countyid using `k12', nogen
	merge 1:1 countyid using `gap', nogen
	merge 1:1 countyid using `ocr', nogen
	merge 1:1 countyid using `pool', nogen
	
	reshape long ///
		implicitbias households medinc tot wht blk oth male female, ///
		i(countyid geography) j(sample)
		
	egen rm = rowmiss(*)
	keep if rm!=9
	drop rm
	collapse (mean) implicitbias households medinc tot wht blk oth male female ///
		(count) n=countyid, by(sample)
		
